/**
 * Filename: OfficialListAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 16:23
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNCouPon;

import java.util.ArrayList;
import java.util.List;

public class CouponListAdapter extends RecyclerView.Adapter<CouponListAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context context;
    private List<YNCouPon> datas = new ArrayList<>();
    private View mHeaderView;

    public CouponListAdapter(Context context, List<YNCouPon> datas) {
        this.context = context;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView coupon_price_bg;
        TextView coupon_price;
        TextView coupon_name;
        TextView coupon_usetime;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            coupon_price_bg = (ImageView) itemView
                    .findViewById(R.id.coupon_price_bg);
            coupon_price = (TextView) itemView
                    .findViewById(R.id.coupon_price);
            coupon_name = (TextView) itemView
                    .findViewById(R.id.coupon_name);
            coupon_usetime = (TextView) itemView
                    .findViewById(R.id.coupon_usetime);
        }

        public void bindData(final YNCouPon listItems, final int point) {
            if (listItems != null) {
                coupon_price.setText(String.valueOf(listItems.getCouponLimit()));
                switch (listItems.getCouponType()) {
                    case 1:
                        coupon_price_bg.setImageResource(R.drawable.coupon_lomo_bg);
                        break;
                    case 2:
                        coupon_price_bg.setImageResource(R.drawable.coupon_voicecard_bg);
                        break;
                    case 3:
                        coupon_price_bg.setImageResource(R.drawable.coupon_photospic_bg);
                        break;
                    case 4:
                        coupon_price_bg.setImageResource(R.drawable.coupon_pin_bg);
                        break;
                }
                coupon_name.setText(listItems.getCouponName().replace("优惠券", ""));
                coupon_usetime.setText("使用至" + listItems.getCouponDate());
            }
        }
    }


    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private YNCouPon getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_coupon, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        YNCouPon listItems = getItem(pos);
        holder.bindData(listItems, pos);
        holder.itemView.setTag(listItems);
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }


    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }
}
